package com.kidedu.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;



import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.kidedu.service.IKStudentService;
import com.kidedu.util.SpringContextUtil;
import com.opensymphony.xwork2.Action;

import net.sf.json.JSONObject;

public class KStudentBatchImportAction {

         private 	 IKStudentService  kStudentService= (IKStudentService) SpringContextUtil.getBean("iKStudentService");
	    private File file;
	    private  JSONObject jsonObj=new JSONObject();
	    public String BatchImport()
	    {
	    	  Workbook book = null;
		        try {
		            OPCPackage pkg = OPCPackage.openOrCreate(file);
		            book = new XSSFWorkbook(pkg);
		        } catch (Exception ex) {
		            try {
						book = new XSSFWorkbook(new FileInputStream(file));
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		        }
			   try{
			      
				   Sheet sheet=book.getSheetAt(0);		
				  jsonObj=kStudentService.BatchImport(sheet);
			       
			   }
			   catch(Exception e)
			   {
				   System.out.println(e);
			   }
			
			return Action.SUCCESS;
		}
		public File getFile() {
			return file;
		}
		public void setFile(File file) {
			this.file = file;
		}
	
		public JSONObject getJsonObj() {
			return jsonObj;
		}
		public void setJsonObj(JSONObject jsonObj) {
			this.jsonObj = jsonObj;
		}
}
